package ca.uwindsor.cs.deepweb.utility;

import java.io.File;

public class DocumentRegrouper {

	/**
	 * The prefix of the filename
	 */
	protected String prefix;

	/**
	 * The extension of the file
	 */
	protected String extension;

	/**
	 * The volumn ID after prefix, 3 digits
	 */
	protected short volumnID;

	/**
	 * The group ID after volumn ID, 2 digits
	 */
	protected short groupID;

	/**
	 * The file ID after group ID, 4 digits
	 */
	protected short fileID;

	protected java.text.DecimalFormat fileIDformater;

	protected java.text.DecimalFormat groupIDformater;

	protected java.text.DecimalFormat volumnIDformater;

	protected File sourceDir;

	protected File destinationDir;

	protected boolean recursive;

	/**
	 * Construct a new instance of DocumentRegrouper to run independently
	 * 
	 * @param source
	 *            Source directory that contains all files
	 * @param destination
	 *            Destinate directory, renamed files will be saved in here and
	 *            sub-directories will be created
	 * @param prefix
	 *            The prefix of the file name
	 * @param ext
	 *            The extension of the file name
	 */
	public DocumentRegrouper(String source, String destination, String prefix,
			String ext) {
		this.prefix = prefix;
		extension = ext;
		groupID = 0;
		volumnID = 0;
		fileID = 0;
		fileIDformater = new java.text.DecimalFormat("0000");
		groupIDformater = new java.text.DecimalFormat("00");
		volumnIDformater = new java.text.DecimalFormat("000");
		sourceDir = new File(source);
		destinationDir = new File(destination);
		recursive = false;
	}

	/**
	 * @param prefix
	 * @param ext
	 */
	public DocumentRegrouper(String prefix, String ext) {
		this.prefix = prefix;
		extension = ext;
		groupID = 0;
		volumnID = 0;
		fileID = 0;
		fileIDformater = new java.text.DecimalFormat("0000");
		groupIDformater = new java.text.DecimalFormat("00");
		volumnIDformater = new java.text.DecimalFormat("000");
	}

	protected void regroup(File sdir) {

		File[] dataFiles = sdir.listFiles();
		for (int i = 0; i < dataFiles.length; i++) {
			File newfile = dataFiles[i];
			if (newfile.isFile()) {
				String newfilename = getFilename();
				// File dest = new File(dir, getFilename());
				File dir = new File(destinationDir, prefix + getVolumnID()
						+ File.separator + getGroupID());
				if (dir.exists() == false) {
					dir.mkdirs();
				}
				File dest = new File(dir, newfilename);
				newfile.renameTo(dest);

				try {
					System.out
							.println("Renamed to: " + dest.getCanonicalPath());
				} catch (Exception e) {
					e.printStackTrace();
					System.exit(-1);
				}

				if (fileID == 3000) {
					addGroupID();
					fileID = 0;
				}
				if (groupID == 100) {
					addVolumnID();
					groupID = 0;
				}
				if (volumnID == 1000) {
					System.err.println("Too many documents");
					System.exit(-1);
				}

			} else if (newfile.isDirectory()) {
				if (recursive) {
					regroup(newfile);
				}
			}
		}

	}

	public void regroup() {
		regroup(sourceDir);
	}

	public String getFilename() {
		String fid = getFileID();
		String filename = new String(extension);
		filename = "." + filename;
		filename = fid + filename;
		filename = "-" + filename;
		filename = getGroupID() + filename;
		filename = "-" + filename;
		filename = getVolumnID() + filename;
		filename = prefix + filename;

		return filename;
	}

	public String getDirectories() {
		String newdir = new String(prefix + getVolumnID() + File.separator
				+ getGroupID() + File.separator);
		if (fileID == 3000) {
			addGroupID();
			fileID = 0;
		}
		if (groupID == 100) {
			addVolumnID();
			groupID = 0;
		}
		if (volumnID == 1000) {
			System.err.println("Too many documents");
			// System.exit(-1);
			return null;
		}
		return newdir;
	}

	protected String getFileID() {
		String fileid = fileIDformater.format(fileID);
		addFileID();
		return fileid;
	}

	protected String getGroupID() {
		String groupid = groupIDformater.format(groupID);
		// addGroupID();
		return groupid;
	}

	protected String getVolumnID() {
		return volumnIDformater.format(volumnID);
	}

	protected void addFileID() {
		fileID++;
		// if (fileID == 10) {
		// addGroupID();
		// fileID = 0;
		// }
	}

	protected void addGroupID() {
		groupID++;
		// if (groupID == 100) {
		// addVolumnID();
		// groupID = 0;
		// }
	}

	protected void addVolumnID() {
		volumnID++;

	}

	public void setRecursive(boolean flag) {
		recursive = flag;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// DocumentRegrouper dr = new DocumentRegrouper(
		// "D:\\Research\\estimation\\datasource\\NG\\toindex",
		// "D:\\Research\\estimation\\datasource\\NG_regrouped",
		// "NG", "txt");
		// dr.setRecursive(true);
		// dr.regroup();

		// DocumentRegrouper dr = new DocumentRegrouper(
		// "D:\\学习资料\\Graduate Study\\Research\\Data\\Reuters\\test1",
		// "D:\\学习资料\\Graduate Study\\Research\\Data\\Reuters\\dest",
		// "Newsgroup", "txt");
		// dr.regroup();
		if (args.length != 4) {
			System.out
					.println("java DocumentRegrouper SourceDIR DestinationDIR prefix extension");
//			DocumentRegrouper dr = new DocumentRegrouper("test", "t");
//			for (int i = 0; i < 10001; i++) {
//				System.out.println(dr.getFilename());
//				System.out.println(dr.getDirectories());
//			}
		} else {
			DocumentRegrouper dr = new DocumentRegrouper(args[0], args[1],
					args[2], args[3]);
			dr.regroup();
		}
	}

}
